using UnityEngine;
using System.Collections;

public class MScrollView : MControl
{
	public GameObject prefabControl;
	public float magin;
	public bool Visible;
	public int visibleItemCount;
		
	protected bool bMove = false;
	
	ArrayList m_Items = new ArrayList();
	Vector2 currentPos = new Vector2(0,0);
	Vector2 maxPos = new Vector2(0,0);
	
	Vector2 beginpos = Vector2.zero;
	
	void Start () 
	{
		base.Start();
		Enable = Visible;
	}
	 
	public void SetEnable(bool b)
	{
		gameObject.renderer.enabled = b;
		foreach(MScrollViewItem2 item in m_Items)
		{
			item.SetEnable(b);
			item.Move(new Vector2(0,0));
			//item
		}
	}
	
	public bool GetEnable()
	{
		return gameObject.renderer.enabled;
	}
	
	public bool Down
	{
		set	{ this.bDown = value; }
		get { return this.bDown; }
	}
	
	public bool Enable
	{
		set
		{
			this.Visible = value;
			gameObject.renderer.enabled = value;
			foreach(MScrollViewItem2 item in m_Items)
			{
				item.SetEnable(value);
			}
		}
		
		get { return this.Visible; }
	}

	public virtual void Update()
	{
		if( gameObject.renderer.enabled == false )
		{
			foreach(MScrollViewItem2 item in m_Items)
			{
				item.gameObject.renderer.enabled = false;
			}
			return;
		}
		if( touchPhase == TouchPhase.Moved )
		{
			if( bDown == true )
			{
				bDown = false;
			}
		}
	}
	
	public virtual void OnEvent(Touch t)
	{
		//return;
		if( gameObject.renderer.enabled == false )
			return;
		
		touchPhase = t.phase;
		if( t.phase == TouchPhase.Began )
		{
			OnDown(t);
		}
		else if( t.phase == TouchPhase.Moved )
		{
			OnMove(t);
		}
		else if( t.phase == TouchPhase.Ended )
		{
			OnUp(t);
		}
	}

    public MScrollViewItem2 AddItem(float x, float y, int ID, Texture view1, Texture view2)
    {
        GameObject go = Instantiate(prefabControl) as GameObject;
        MScrollViewItem2 cb = (MScrollViewItem2)go.GetComponent(typeof(MScrollViewItem2));
		cb.Id = ID;
		cb.view = view1;
		cb.view2 = view2;
        cb.x = this.x + x + magin;
        cb.y = this.y + y + magin;
        cb.depth = this.depth+1;
		cb.p = this;
        cb.Start();
        m_Items.Add(cb);
		
		float count = m_Items.Count - visibleItemCount;
		if( count > 0 )
		{
			maxPos.y = count * (cb.tY+magin);
		}
		
		return cb;
    }
	
	public bool IsMove()
	{
		return bMove;
	}

	public virtual int OnMove(Touch t)
	{
		if( bDown == true )
		{
			if( bMove == false )
			{
				MUIManager.Instance().SetScrollView(this);
				bMove = true;
				beginpos = t.position;
			}
		}
		
		if( bMove == true )
		{
			currentPos.y += t.deltaPosition.y;
			if( currentPos.y < 0 )
			{
				currentPos.y = 0;
			}
			if( currentPos.y > maxPos.y )
			{
				currentPos.y = maxPos.y;
			}
			
			foreach(MScrollViewItem2 item in m_Items)
			{
				item.Move(currentPos);
			}
		}
		
		return 0;
	}
	
	public virtual void Move(Vector2 v)
	{
		currentPos.y += v.y;
		if( currentPos.y < 0 )
		{
			currentPos.y = 0;
		}
		if( currentPos.y > maxPos.y )
		{
			currentPos.y = maxPos.y;
		}
		
		foreach(MScrollViewItem2 item in m_Items)
		{
			item.Move(currentPos);
		}
	}
	
	public int GetItemCount()
	{
		return m_Items.Count;
	}
}
